package webjoch.methods;

import java.awt.Point;
import java.util.HashMap;

import webjoch.Main;
import webjoch.misc.Functions;
import webjoch.wrappers.Door;
import webjoch.wrappers.Link;
import webjoch.wrappers.Room;

import com.rsbuddy.script.methods.Widgets;
import com.rsbuddy.script.wrappers.Component;
import com.rsbuddy.script.wrappers.Tile;

public class Map {
	public static final int WIDGET_ID = 942;
	public static final int[] LOCKED_ROOMS_TEXTURES = {2787, 2788, 2789, 2790};
	
	private static HashMap<Integer, boolean[]> rooms = new HashMap<Integer, boolean[]>();
	
	public static void init(){
		rooms.put(2791, new boolean[]{false, false, false, true});
		rooms.put(2792, new boolean[]{true, false, false, false});
		rooms.put(2794, new boolean[]{false, false, true, false});
		rooms.put(2796, new boolean[]{true, true, false, false});
		rooms.put(2797, new boolean[]{false, true, true, false});
		rooms.put(2798, new boolean[]{false, false, true, true});
		rooms.put(2799, new boolean[]{true, true, false, true});
		rooms.put(2800, new boolean[]{true, true, true, false});
		rooms.put(2801, new boolean[]{false, true, true, true});
		rooms.put(2802, new boolean[]{true, false, true, true});
		rooms.put(2803, new boolean[]{true, true, true, true});
		rooms.put(2804, new boolean[]{false, true, false, true});
		rooms.put(2805, new boolean[]{true, false, true, false});
		//rooms.put(, new boolean[]{false, false, false, false});
	}
	
	public static void buildMap(){
		if (rooms.size() == 0) init();
		Main.getDungeon().clearMap();
		
		for (Component roomComp : Widgets.getComponent(WIDGET_ID, 3).getComponents()){
			boolean[] doors = rooms.get(roomComp.getTextureId());
			if (doors != null){
				Point p = roomComp.getRelLocation();
				for (int i = 0; i < 4; i++){
					if (doors[i]){
						Point linkedP = p;
						if (i == 0)  linkedP = new Point(p.x - 32, p.y);
						if (i == 1)  linkedP = new Point(p.x, p.y - 32);
						if (i == 2)  linkedP = new Point(p.x + 32, p.y);
						if (i == 3)  linkedP = new Point(p.x, p.y + 32);
						
						if (!isLockedRoom(linkedP)){
							Room room = Rooms.getRoom(pointToTile(p));
							Room linkedRoom = Rooms.getRoom(pointToTile(linkedP));
							Main.getDungeon().addRoom(new Link(room, getRotation(i), linkedRoom));
						}
					}
				}
			}
		}
	}
	
	public static boolean isLockedRoom(Point p){
		for (Component roomComp : Widgets.getComponent(WIDGET_ID, 3).getComponents()){
			if (roomComp.getRelLocation().equals(p) && Functions.inArray(LOCKED_ROOMS_TEXTURES, roomComp.getTextureId())){
				return true;
			}
		}
		return false;
	}
	
	public static Tile pointToTile(Point p){
		Tile baseTile = Main.getDungeon().getBaseLocation();
		Point base = Widgets.getComponent(WIDGET_ID, 4).getComponent(0).getRelLocation();
		double roomX = (p.x - base.x) / 32;
		double roomY = (p.y - base.y) / 32;
		return new Tile(baseTile.getX() + (int) (roomX * 15), baseTile.getY() - (int) (roomY * 15));
	}
	

	
	public static Door.Rotation getRotation(int rotation){
		if (rotation == 0) return Door.Rotation.WEST;
		if (rotation == 1) return Door.Rotation.NORTH;
		if (rotation == 2) return Door.Rotation.EAST;
		if (rotation == 3) return Door.Rotation.SOUTH;
		return null;
	}
}
